//
//  PerLineBTViewController.m
//  快踩黑块儿
//
//  Created by Darry on 15/3/7.
//  Copyright (c) 2015年 戴. All rights reserved.
//

#import "PerLineBTViewController.h"
#import "LineBt.h"

@interface PerLineBTViewController ()
@property (nonatomic,retain)LineBt *line1;
@property (nonatomic,retain)LineBt *line2;
@property (nonatomic,retain)LineBt *line3;
@property (nonatomic,retain)LineBt *line4;
@property (nonatomic,retain)LineBt *line5;
@end

@implementation PerLineBTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    // Do any additional setup after loading the view.
    //布局视图
    [self layoutView];
    //布局点击事件
    [self layoutTarget];
}
//布局视图
-(void)layoutView{
    self.line1 = [[LineBt alloc]initWithFrame:CGRectMake(0, -142, 320, 142)];
    [_line1 changeblack];
    [self.view addSubview:_line1];
    [_line1 release];
    
    self.line2 = [[LineBt alloc]initWithFrame:CGRectMake(0, 0, 320, 142)];
    [_line2 changeblack];
    [self.view addSubview:_line2];
    [_line2 release];
    
    self.line3 = [[LineBt alloc]initWithFrame:CGRectMake(0, 142, 320, 142)];
    [_line3 changeblack];
    [self.view addSubview:_line3];
    [_line3 release];
    
    self.line4 = [[LineBt alloc]initWithFrame:CGRectMake(0, 284, 320, 142)];
    [_line4 changeblack];
    [self.view addSubview:_line4];
    [_line4 release];
    
    self.line5 = [[LineBt alloc]initWithFrame:CGRectMake(0, 426, 320, 142)];
    [_line5 changeblack];
    [self.view addSubview:_line5];
    [_line5 release];
}
//布局点击事件
-(void)layoutTarget{
    [_line2.bt1 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line2.bt2 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line2.bt3 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line2.bt4 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    
    [_line3.bt1 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line3.bt2 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line3.bt3 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line3.bt4 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    
    [_line4.bt1 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line4.bt2 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line4.bt3 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [_line4.bt4 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    
    [_line5.bt1 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt2 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt3 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt4 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//点击规则:
//(1)不是在最下面一行: 错误的点击
-(void)wrong:(MyButton*)bt{
    if (!bt.black) {
        [UIView beginAnimations:nil context:self];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationRepeatCount:3];
        bt.backgroundColor = [UIColor redColor];
        bt.backgroundColor = [UIColor whiteColor];
        [UIView commitAnimations];
    }
}
//(2)是最下面一行: 判断是否点中了黑块
-(void)judge:(MyButton*)bt{
    if (bt.black) {
       //动画: 黑块变白块
        [UIView beginAnimations:nil context:self];
        [UIView setAnimationDuration:0.1];
        bt.backgroundColor = [UIColor whiteColor];
        bt.backgroundColor = [UIColor blackColor];
        bt.backgroundColor = [UIColor whiteColor];
        [UIView commitAnimations];
       //执行成功规则
        [self bingo:bt];
    }else
    {
        //执行失败规则
        [self wrong:bt];
    }
}
-(void)bingo:(MyButton*)bt{
    NSLog(@"bingo!");
    //    CGPoint center = self.center;
    //    [UIView beginAnimations:@"position" context:self];
    //    [UIView setAnimationDuration:0.5];
    //    self.view.center = CGPointMake(center.x, center.y + 142);
    //    [UIView commitAnimations];
//    CGFloat lo = _line1.frame.origin.y;
//    LineBt *temp0 = [[LineBt alloc]init];
//    temp0.frame = CGRectMake(0, _line1.frame.origin.y, 320, 142);
//    [temp0 changeblack];
    
    

    [UIView beginAnimations:@"position" context:self];
    [UIView setAnimationDuration:0.3];
    
    
    //坐标下移 色块不变
    _line1.frame  = CGRectMake(0, 0, 320, 142) ;
    _line2.frame  = CGRectMake(0, 142, 320, 142) ;
    _line3.frame  = CGRectMake(0, 284, 320, 142) ;
    _line4.frame  = CGRectMake(0, 426, 320, 142) ;
    _line5.frame  = CGRectMake(0, 426 + 142, 320, 142) ;
    [UIView commitAnimations];
//    [self showchange];
//}
//-(void)showchange{
//
    [UIView beginAnimations:@"position" context:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0.01f];
//    //坐标上移 色块相对下移
//    _line5.backgroundColor = _line4.backgroundColor;
//    _line5.frame = CGRectMake(0, 426, 320, 142);
//    _line4.backgroundColor = _line3.backgroundColor;
//    _line4.frame = CGRectMake(0, 284, 320, 142) ;
//    _line3.backgroundColor = _line2.backgroundColor;
//    _line3.frame = CGRectMake(0, 142, 320, 142);
//    _line2.backgroundColor = _line1.backgroundColor;
//    _line2.frame = CGRectMake(0, 0, 320, 142);
//    LineBt *temp1 = _line1;
//
//    _line1 = temp0;
//    [_line5 removeFromSuperview];
    _line5 = _line4;
    [_line5.bt1 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt2 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt3 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    [_line5.bt4 addTarget:self action:@selector(judge:) forControlEvents:UIControlEventTouchUpInside];
    _line4 = _line3;
    _line3 = _line2;
    _line2 = _line1;
    LineBt *temp0 = [[LineBt alloc]initWithFrame:CGRectMake(0, -142, 320, 142)];
    [temp0 changeblack];
    [temp0.bt1 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [temp0.bt2 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [temp0.bt3 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [temp0.bt4 addTarget:self action:@selector(wrong:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:temp0];
    _line1 = temp0;
    
    [UIView commitAnimations];
    
//    [UIView beginAnimations:@"position" context:self];
//    [UIView setAnimationDuration:0.5];
//    [UIView setAnimationDelay:0.5f];
//    [_line1 changeblack];
//    _line1.frame  = CGRectMake(0, _line1.frame.origin.y - 142, 320, 142) ;
//    
//
//    [UIView commitAnimations];
    
    
    
//    [UIView beginAnimations:@"position" context:self];
//    [UIView setAnimationDuration:0.2];
//    [UIView setAnimationBeginsFromCurrentState:YES];
//    [_line5 changeblack];
//    _line5.frame = CGRectMake(0, lo, 320, 142) ;
//     [UIView commitAnimations];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
